package com.centaline.androidsalesblog.activities.newest;

import android.content.Intent;

import com.android.volley.VolleyError;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.NewEstActAdapter;
import com.centaline.androidsalesblog.api.newest.NewEstActListApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.databaseobject.BizUnitDo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstActDo;
import com.centaline.androidsalesblog.model.responseobject.NewEstActListRo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.centanet.framework.interfaces.IResponseListener;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;
import java.util.Locale;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 新盘活动列表
 * Created by guilin on 16/2/23.
 */
public class NewEstActListActivity extends BaseActivity {

    private MRecyclerView mRecyclerView;

    private NewEstActAdapter newEstActAdapter;
    private ArrayList<NewEstActDo> list = new ArrayList<>();
    private DrawableRequestBuilder<String> requestBuilder;

    private int iRefreshType = 1;
    private NewEstActListApi newEstActListApi;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_new_est_act_list;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_new_est_act_list);
        mRecyclerView = (MRecyclerView) findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initViews() {
        requestBuilder = GlideLoad.init(this);
        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                newEstActListApi.setStartIndex(0);
                request();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                newEstActListApi.setStartIndex(list.size());
                request();
            }

            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(NewEstActListActivity.this, NewEstActDetailActivity.class)
                        .putExtra(ExtraConstant.NewEstActId, list.get(position).getActId()));
            }
        });
        newEstActAdapter = new NewEstActAdapter(this, requestBuilder, list);
        mRecyclerView.setAdapter(newEstActAdapter);
        newPropImg();

        newEstActListApi = new NewEstActListApi(NewEstActListActivity.class, NewEstActListRo.class, new IResponseListener<NewEstActListRo>() {
            @Override
            public void onResponse(NewEstActListRo newEstActListRo) {
                final ArrayList<NewEstActDo> temp = newEstActListRo.getList();
                if (iRefreshType == 1) {
                    list.clear();
                }
                if (temp == null) {
                    mRecyclerView.stopRefresh(false);
                    snack(newEstActListRo.getTotal() == 0 ?
                            getString(R.string.snack_new_activity_nothing) :
                            String.format(Locale.CHINA, getString(R.string.snack_new_activity), newEstActListRo.getTotal()));
                } else {
                    list.addAll(temp);
                    mRecyclerView.stopRefresh(temp.size() == 10);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                mRecyclerView.refreshError();
                netWorkUnable();
            }
        });

        mRecyclerView.startRefresh();
    }

    private void request() {
        newEstActListApi.startRequest();
    }

    private void newPropImg() {
        RxDataBase.getBizUnit()
                .compose(this.<BizUnitDo>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BizUnitDo>() {
                    @Override
                    public void call(BizUnitDo bizUnitDo) {
                        newEstActAdapter.setNewPropImg(bizUnitDo.getNewpropimg());
                    }
                });
    }
}
